package com.woniuxy.yoga.evaluate.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.evaluate.model.form.evaluation.*;
import com.woniuxy.yoga.evaluate.model.param.evaluation.*;
import com.woniuxy.yoga.evaluate.service.EvaluationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 评价商家表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/evaluation")
@Api(tags = "评价相关接口")
public class EvaluationController {
    @Resource
    private EvaluationService evaluationService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 用户评价服务
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/addServiceEvaluation.us")
    @ApiOperation("新增服务评价接口")
    @YogaLog("新增服务评价接口")
    public Result<String> addFoodEvaluation(@RequestBody @Valid AddServiceEvaluationForm form, @RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer userId = JWTUtil.getUserId(refreshToken);
        AddServiceEvaluationParam param = BeanUtil.toBean(form, AddServiceEvaluationParam.class);
        param.setUserId(userId);
        evaluationService.addServiceEvaluation(param);
        return Result.getResult(String.class).setMessage("用户评论成功");
    }

    /**
     * 自动评价服务
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/addServiceEvaluationAuto")
    @ApiOperation("自动评价服务操作")
    @YogaLog("自动评价服务操作")
    public Result<String> addServiceEvaluationAuto(@RequestBody @Valid AddServiceEvaluationForm form) throws Exception {
        AddServiceEvaluationParam param = BeanUtil.toBean(form, AddServiceEvaluationParam.class);
        evaluationService.addServiceEvaluation(param);
        return Result.getResult(String.class).setMessage("自动评价成功");
    }

    /**
     * 商家回复用户评价
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/replyEvaluation.bu")
    @ApiOperation("回复评价接口")
    @YogaLog("回复评价接口")
    public Result<String> replyEvaluation(@RequestBody @Valid ReplyEvaluationForm form, @RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        ReplyEvaluationParam param = BeanUtil.toBean(form, ReplyEvaluationParam.class);
        param.setAccountId(businessId);
        evaluationService.replyEvaluation(param);
        return Result.getResult(String.class).setMessage("商家回复用户评论成功");
    }

    /**
     * 用户删除自己的评价
     * @param evaluationId
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteEvaluation.us")
    @ApiOperation("删除我的评价接口")
    @YogaLog("删除我的评价接口")
    public Result<String> deleteEvaluation(Integer evaluationId,@RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer userId = JWTUtil.getUserId(refreshToken);
        evaluationService.deleteEvaluation(evaluationId,userId);
        return Result.getResult(String.class).setMessage("删除评价成功");
    }

    /**
     * 用户查看商家所有评价
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/selEvaluation.us")
    @ApiOperation("用户分页查询门店所属评价")
    @YogaLog("用户分页查询门店所属评价")
    public Result<PageInfo> selEvaluation(@RequestBody SelEvaluationForm form,@RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer userId = JWTUtil.getUserId(refreshToken);
        SelEvaluationParam param = BeanUtil.toBean(form, SelEvaluationParam.class);
        param.setUserId(userId);
        PageInfo pageInfo = evaluationService.selEvaluation(param);
        return Result.getResult(PageInfo.class).setMessage("用户查看门店评价成功").setData(pageInfo);
    }

    /**
     * 商家查看用户评论
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/selEvaluation.bu")
    @ApiOperation("商家分页查询门店所属评价")
    @YogaLog("商家分页查询门店所属评价")
    public Result<PageInfo> buselEvaluation(@RequestBody BuSelEvaluationForm form, @RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        BuSelEvaluationParam param = BeanUtil.toBean(form, BuSelEvaluationParam.class);
        param.setAccountId(businessId);
        PageInfo pageInfo = evaluationService.buselEvaluation(param);
        return Result.getResult(PageInfo.class).setMessage("商家查看门店评价成功").setData(pageInfo);
    }

    /**
     * 用户分页查询自己的评论
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/selUserEvaluation.us")
    @ApiOperation("用户查看自己的评论")
    @YogaLog("用户查看自己的评论")
    public Result<PageInfo> selUserEvaluation(@RequestBody SelUserEvaluationForm form, @RequestHeader String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer userId = JWTUtil.getUserId(refreshToken);
        SelUserEvaluationParam param = BeanUtil.toBean(form, SelUserEvaluationParam.class);
        param.setUserId(userId);
        PageInfo pageInfo = evaluationService.selUserEvaluation(param);
        return Result.getResult(PageInfo.class).setMessage("用户查看自己评论成功").setData(pageInfo);
    }
}

